#!usr/bin/env python3
#定义各种装饰器
from .models import Permission
from functools import wraps
from flask import abort,redirect,url_for
from flask_login import current_user
import time

def premission_required(permission): #有权限限制的装饰器(带参数的装饰器定义)
	def decorator(f):
		@wraps(f) #防止函数被重命名
		def decorator_func(*args,**kwargs):
			if not current_user.can(permission):
				abort(403)
			return f(*args,**kwargs)
		return decorator_func
	return decorator 

#管理员限制的装饰器
def admin_required(f):
	return premission_required(Permission.ADMIN)(f)

#计算函数执行时间的装饰器
def count_fn_time(f):
	@wraps(f)
	def fun_dec(*args,**kwargs):
		st = time.time()
		ret = f(*args,**kwargs)
		dif = time.time()-st
		print('函数',f.__name__,'执行完成共耗时',dif,'毫秒')
		return ret
	return fun_dec
		

